//
//  RepealViewController.m
//  iStore
//
//  Created by Geek on 15/4/22.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "RepealViewController.h"
#import "RequestEngine.h"
#import "QueryCancelModel.h" 


@interface RepealViewController ()

{
    NSString *_userID;

}

@property (weak, nonatomic) IBOutlet UILabel *releasedAmount;
@property (weak, nonatomic) IBOutlet UILabel *releasedMoney;
@property (weak, nonatomic) IBOutlet UILabel *unReleasedAmount;
@property (weak, nonatomic) IBOutlet UILabel *unReleasedMoney;
@property (weak, nonatomic) IBOutlet UILabel *cancelAbleAmount;
@property (weak, nonatomic) IBOutlet UILabel *cancelAbleMoney;
@property (weak, nonatomic) IBOutlet UILabel *orderFormNumber;
@property (strong, nonatomic) UIAlertView *alert;

@end

@implementation RepealViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"撤销订单";
   
    [_cancelButton setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    
    NSDictionary *dict = @{@"orderFormNumber":_orderType};
    
    [RequestEngine queryCancelADWithDic:dict completed:^(NSString *errorcode, NSDictionary *resultDict) {
    
        QueryCancelModel *cancel=[[QueryCancelModel alloc]init];
        [cancel queryCancelADModel:resultDict];
        [self refresh:cancel];
    }];
    
}
//releasedAmount releasedMoney unReleasedAmount unReleasedMoney cancelAbleAmount cancelAbleMoney

// 显示撤销数据
- (void)refresh:(QueryCancelModel*)cancel
{
    
    _orderFormNumber.text = _orderType;
    _releasedAmount.text = [NSString stringWithFormat:@"%ld",(long)[cancel releasedAmount]];
    _releasedMoney.text = [NSString stringWithFormat:@"%.2f",[cancel releasedMoney]];
    _unReleasedAmount.text = [NSString stringWithFormat:@"%ld",(long)[cancel unReleasedAmount]];
    _unReleasedMoney.text = [NSString stringWithFormat:@"%.2f",[cancel unReleasedMoney]];
    _cancelAbleAmount.text = [NSString stringWithFormat:@"%ld",(long)[cancel cancelAbleAmount]];
    _cancelAbleMoney.text = [NSString stringWithFormat:@"%.2f",[cancel cancelAbleMoney]];
    
}

// 确定撤销
- (IBAction)Cancel:(UIButton *)sender {
    
    if (_alert == nil) {
        _alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定撤销吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    }
    [_alert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {
        _userID = [[NSString alloc]init];
        _userID = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
        
        NSDictionary *dict = @{@"userID":_userID,@"appKey":@"IOS",@"orderFormNumber":_orderType,@"cancelAbleMoney":_cancelAbleMoney.text,@"cancelAbleAmount":_cancelAbleAmount.text};
        [self activityStart];
        [RequestEngine cancelADWithDic:dict completed:^(NSString *errorcode, NSDictionary *resultDict) {
            
            
            [self activityStop];
            if ([errorcode isEqualToString:@"0"]) {
                alert(@"撤销成功");
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    else {
        return;
    }
    
}

@end
